﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
using YbxLibrary.entity;

namespace YbxLibrary
{
    public class CallYb
    {
        /// <summary>
        /// 直接调用医保动态库
        /// </summary>
        /// <param name="astr_jybh">交易编号</param>
        /// <param name="input">交易输入</param>
        /// <returns></returns>
        private static YbEntity HandleYhHbCall(String astr_jybh, String input)
        {
            String astr_jysr = input;
            string currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
            LogHelper.Debug("【" + astr_jybh + "】原始输入:" + input);
            LogHelper.Info("【" + astr_jybh + "】交易输入:" + astr_jysr);
            YbEntity entity = new YbEntity();

            entity.infcode = -1;
            entity.output = ""; 
            entity.err_msg = "初始值";

            try
            {
                var yhType = Type.GetTypeFromProgID("yinhai.yh_hb_sctr");
                var yhObject = System.Activator.CreateInstance(yhType);
                string astr_jysc = String.Empty;

                var args = new object[] { astr_jybh, astr_jysr, astr_jysc };
                var parameterModifier = new ParameterModifier(3);
                parameterModifier[0] = false;
                parameterModifier[1] = false;
                parameterModifier[2] = true;
                var parameterModifiers = new ParameterModifier[] { parameterModifier };

                yhType.InvokeMember("yh_hb_call", BindingFlags.InvokeMethod, null, yhObject, args, parameterModifiers, System.Globalization.CultureInfo.CurrentCulture, null);

                entity.astr_jybh = args[0].ToString();
                entity.astr_jysr = args[1].ToString();
                entity.astr_jysc = args[2].ToString();

                yhType = null;
                yhObject = null;

                LogHelper.Info("【" + astr_jybh + "】交易输出:" + entity.astr_jysc);
                if (!"".Equals(entity.astr_jysc))
                {
                    try
                    {
                        JObject jsonObject = JObject.Parse(entity.astr_jysc);
                        try
                        {
                            string infcode = jsonObject["infcode"].ToString();
                            string err_msg = jsonObject["err_msg"].ToString();
                            entity.infcode = Convert.ToInt32(infcode);
                            entity.err_msg = err_msg;
                        }
                        catch (Exception exxxx)
                        {
                            Console.WriteLine(exxxx.Message);
                        }

                        try
                        {
                            string output = jsonObject["output"].ToString();
                            entity.output = output;
                        }
                        catch (Exception exxxx)
                        {
                            Console.WriteLine(exxxx.Message);
                        }

                        if (entity.infcode == 0)
                        {
                            LogHelper.Debug("【" + astr_jybh + "】内容输出:" + entity.output);
                        }
                        else
                        {
                            LogHelper.Error("【" + astr_jybh + "】内容输出:" + entity.astr_jysc);
                        }
                    }
                    catch (Exception exx)
                    {
                        LogHelper.Error("【" + astr_jybh + "】交易出参解析出错:" + exx.Message, exx);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("【" + astr_jybh + "】交易调用出错:" + ex.Message, ex);
                entity.astr_jybh = astr_jybh;
                entity.astr_jysr = astr_jysr;
                entity.astr_jysc = "交易调用出错" + ex.Message;
            }
            return entity;
        }
        /// <summary>
        /// 调用医保交易
        /// </summary>
        /// <param name="astr_jybh">交易编号</param>
        /// <param name="astr_jysr">交易输入</param>
        /// <returns></returns>
        private static YbEntity HandleYhHbCall(String astr_jybh, String input, String opter_input, String opter_name_input, String mdtrtarea_admvs_input, String insuplc_admdvs_input, String sign_no_input)
        {
            String astr_jysr = JsonStringBuilder.BuildJsonString(astr_jybh, input, opter_input, opter_name_input, mdtrtarea_admvs_input, insuplc_admdvs_input, sign_no_input);
            string currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
            LogHelper.Debug("【" + astr_jybh + "】原始输入:" + input);
            LogHelper.Info("【" + astr_jybh + "】交易输入:" + astr_jysr);
            YbEntity entity = new YbEntity();

            entity.infcode = -1;
            entity.output = "";
            entity.err_msg = "初始值";

            try
            {
                var yhType = Type.GetTypeFromProgID("yinhai.yh_hb_sctr");
                var yhObject = System.Activator.CreateInstance(yhType);
                string astr_jysc = String.Empty;

                var args = new object[] { astr_jybh, astr_jysr, astr_jysc };
                var parameterModifier = new ParameterModifier(3);
                parameterModifier[0] = false;
                parameterModifier[1] = false;
                parameterModifier[2] = true;
                var parameterModifiers = new ParameterModifier[] { parameterModifier };

                yhType.InvokeMember("yh_hb_call", BindingFlags.InvokeMethod, null, yhObject, args, parameterModifiers, System.Globalization.CultureInfo.CurrentCulture, null);

                entity.astr_jybh = args[0].ToString();
                entity.astr_jysr = args[1].ToString();
                entity.astr_jysc = args[2].ToString();

                yhType = null;
                yhObject = null;

                LogHelper.Info("【" + astr_jybh + "】交易输出:" + entity.astr_jysc);
                if (!"".Equals(entity.astr_jysc))
                {
                    try
                    {
                        JObject jsonObject = JObject.Parse(entity.astr_jysc);
                        try
                        {
                            string infcode = jsonObject["infcode"].ToString();
                            string err_msg = jsonObject["err_msg"].ToString();
                            entity.infcode = Convert.ToInt32(infcode);
                            entity.err_msg = err_msg;
                        }
                        catch (Exception exxxx)
                        {
                            Console.WriteLine(exxxx.Message);
                        }

                        try
                        {
                            string output = jsonObject["output"].ToString();
                            entity.output = output;
                        }
                        catch (Exception exxxx)
                        {
                            Console.WriteLine(exxxx.Message);
                        }

                        if (entity.infcode == 0)
                        {
                            LogHelper.Debug("【" + astr_jybh + "】内容输出:" + entity.output);
                        }
                        else
                        {
                            LogHelper.Error("【" + astr_jybh + "】内容输出:" + entity.astr_jysc);
                        }
                    }
                    catch (Exception exx)
                    {
                        LogHelper.Error("【" + astr_jybh + "】交易出参解析出错:" + exx.Message, exx);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("【" + astr_jybh + "】交易调用出错:" + ex.Message, ex);
                entity.astr_jybh = astr_jybh;
                entity.astr_jysr = astr_jysr;
                entity.astr_jysc = "交易调用出错" + ex.Message;
            }

            return entity;
        }
        /// <summary>
        /// 无依赖
        /// </summary>
        /// <param name="astr_jybh">接口代码</param>
        /// <param name="input">输入字符串</param>
        /// <param name="opter_input">用户工号</param>
        /// <param name="opter_name_input">用户姓名</param>
        /// <param name="mdtrtarea_admvs_input">就医地</param>
        /// <param name="insuplc_admdvs_input">参保地</param>
        /// <param name="sign_no_input">签到编号</param>
        /// <returns></returns>
        private static YbEntity HandleYhHbCall_WYL(String astr_jybh, String input, String opter_input, String opter_name_input, String mdtrtarea_admvs_input, String insuplc_admdvs_input, String sign_no_input)
        {
            String astr_jysr = JsonStringBuilder.BuildJsonString(astr_jybh, input, opter_input, opter_name_input, mdtrtarea_admvs_input, insuplc_admdvs_input, sign_no_input);
            string currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
            YbEntity entity = new YbEntity();
            entity.infcode = -1;
            entity.output = "";
            entity.err_msg = "初始值";
            try
            {
                var yhType = Type.GetTypeFromProgID("yinhai.yh_hb_sctr");
                var yhObject = System.Activator.CreateInstance(yhType);
                string astr_jysc = String.Empty;

                var args = new object[] { astr_jybh, astr_jysr, astr_jysc };
                var parameterModifier = new ParameterModifier(3);
                parameterModifier[0] = false;
                parameterModifier[1] = false;
                parameterModifier[2] = true;
                var parameterModifiers = new ParameterModifier[] { parameterModifier };

                yhType.InvokeMember("yh_hb_call", BindingFlags.InvokeMethod, null, yhObject, args, parameterModifiers, System.Globalization.CultureInfo.CurrentCulture, null);

                entity.astr_jybh = args[0].ToString();
                entity.astr_jysr = args[1].ToString();
                entity.astr_jysc = args[2].ToString();

                yhType = null;
                yhObject = null;
                try
                {
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    Dictionary<string, object> rootObject = serializer.Deserialize<Dictionary<string, object>>(entity.astr_jysc);

                    entity.infcode = Convert.ToInt32(rootObject["infcode"]);
                    entity.err_msg = Convert.ToString(rootObject["err_msg"]);

                    // 取出output节点
                    var outputObject = rootObject["output"] as Dictionary<string, object>;
                    entity.output = Convert.ToString(serializer.Serialize(outputObject));                
                }
                catch (Exception ex111)
                {
                    Console.WriteLine(ex111.Message);
                }
            }
            catch (Exception ex)
            {
                entity.astr_jybh = astr_jybh;
                entity.astr_jysr = astr_jysr;
                entity.astr_jysc = "交易调用出错" + ex.Message;
            }
            return entity;
        }
           
        /// <summary>
        /// 直接调用医保
        /// </summary>
        /// <param name="astr_jybh">交易编号</param>
        /// <param name="input">交易输入</param>
        /// <param name="orig">标志位</param>
        /// <returns></returns>
        public static YbEntity YhHbCall(String astr_jybh, String input, String orig)
        {
            return HandleYhHbCall(astr_jybh, input);
        }
        /// <summary>
        /// 测试使用
        /// </summary>
        /// <param name="astr_jybh">交易编号</param>
        /// <param name="input">交易输入数据体</param>
        /// <returns></returns>
        public static YbEntity YhHbCall(String astr_jybh, String input)
        {
            return HandleYhHbCall(astr_jybh, input, "", "", "", "", "");
        }
        /// <summary>
        /// 本地使用
        /// </summary>
        /// <param name="astr_jybh"></param>
        /// <param name="input"></param>
        /// <param name="opter_input"></param>
        /// <param name="opter_name_input"></param>
        /// <param name="sign_no_input"></param>
        /// <returns></returns>
        public static YbEntity YhHbCall(String astr_jybh, String input, String opter_input, String opter_name_input, String sign_no_input)
        {
            return HandleYhHbCall(astr_jybh, input, opter_input, opter_name_input, "", "", sign_no_input);
        }
        /// <summary>
        /// 异地使用
        /// </summary>
        /// <param name="astr_jybh"></param>
        /// <param name="input"></param>
        /// <param name="opter_input"></param>
        /// <param name="opter_name_input"></param>
        /// <param name="mdtrtarea_admvs_input"></param>
        /// <param name="insuplc_admdvs_input"></param>
        /// <param name="sign_no_input"></param>
        /// <returns></returns>
        public static YbEntity YhHbCall(String astr_jybh, String input, String opter_input, String opter_name_input, String mdtrtarea_admvs_input, String insuplc_admdvs_input, String sign_no_input) {
            return HandleYhHbCall( astr_jybh,  input,  opter_input,  opter_name_input,  mdtrtarea_admvs_input,  insuplc_admdvs_input,  sign_no_input);
        }
        /// <summary>
        /// 无依赖
        /// </summary>
        /// <param name="astr_jybh">方法编号</param>
        /// <param name="input">输入参数</param>
        /// <param name="opter_input">工号</param>
        /// <param name="opter_name_input">姓名</param>
        /// <param name="mdtrtarea_admvs_input">就医地</param>
        /// <param name="insuplc_admdvs_input">参保地</param>
        /// <param name="sign_no_input">签到编号</param>
        /// <returns></returns>
        public static YbEntity YhHbCallNoDependency(String astr_jybh, String input, String opter_input, String opter_name_input, String mdtrtarea_admvs_input, String insuplc_admdvs_input, String sign_no_input)
        {
            return HandleYhHbCall_WYL(astr_jybh, input, opter_input, opter_name_input, mdtrtarea_admvs_input, insuplc_admdvs_input, sign_no_input);
        }
    }
}
